#include "itemwizard.h"
#include <QtGui>

ItemWizard::ItemWizard(QWidget *parent)
  : QWizard(parent) {
  firstpage = new NewItemPage();
  NewFilesPage* getnamepage = new NewFilesPage(this);

  addPage(firstpage);
  addPage(getnamepage);
}

void ItemWizard::disable(int index)
{
  firstpage->disable(index);
}

/****************************
*****************************/

NewItemPage::NewItemPage(QWidget *parent)
  : QWizardPage(parent) {
  setTitle("Add New Item");

  QGroupBox* addbox = new QGroupBox("Select Item");
  QVBoxLayout* boxlayout = new QVBoxLayout();
  addbox->setLayout(boxlayout);
  filetype = new QComboBox();
  filetype->addItem("Go Code", ItemWizard::Go_Page);
  filetype->addItem("Codes Folder", ItemWizard::Codes_Page);
  filetype->addItem("Statics Folder", ItemWizard::Statics_Page);
  filetype->addItem("Template File", ItemWizard::Template_Page);
  filetype->addItem("Other", ItemWizard::Other_Page);
  boxlayout->addWidget(filetype);
  registerField("filetype", filetype);


  QGroupBox* importbox = new QGroupBox("Import File From Disk");
  QLineEdit* filepath = new QLineEdit();
  QPushButton* browser = new QPushButton("Browse File");
  QHBoxLayout* importlayout = new QHBoxLayout();
  importlayout->addWidget(filepath);
  importlayout->addWidget(browser);
  importbox->setLayout(importlayout);

  QVBoxLayout* mainlayout = new QVBoxLayout();
  mainlayout->addWidget(addbox);
  mainlayout->addWidget(importbox);
  this->setLayout(mainlayout);
}

void NewItemPage::disable(int i)
{
  QModelIndex index = filetype->model()->index(i, 0);
  QVariant v(0);
  filetype->model()->setData(index, v, Qt::UserRole -1);
}

/****************************
*****************************/

NewFilesPage::NewFilesPage(QWidget *parent)
  : QWizardPage(parent) {
  errorLabel = new QLabel("Invalid Name.");
  errorLabel->hide();
  folderNameLabel = new QLabel("Name:");

  QLineEdit* folderNameInput = new QLineEdit();
  QGridLayout* grid = new QGridLayout();
  grid->addWidget(folderNameLabel, 0, 0);
  grid->addWidget(folderNameInput, 0, 1);
  grid->addWidget(errorLabel, 1, 0, 1, 2);
  this->setLayout(grid);

  registerField("filename*", folderNameInput);
}

bool NewFilesPage::validatePage()
{
  QRegExp reg;

  if(allowDot)
    reg.setPattern("[A-Za-z_.0-9]*");
  else
    reg.setPattern("[A-Za-z_0-9]*");

  if(reg.exactMatch(field("filename").toString()))
  {
    errorLabel->hide();
    return true;
  } else {
    errorLabel->show();
    return false;
  }
}

void NewFilesPage::initializePage() {
  int type = field("filetype").toInt();
  switch (type) {
    case ItemWizard::Go_Page:
      setTitle("New Go Source File");
      break;
    case ItemWizard::Codes_Page:
      setTitle("New Codes Folder");
      break;
    case ItemWizard::Statics_Page:
      setTitle("New Statics Folder");
      break;
    case ItemWizard::Template_Page:
      setTitle("New Template File");
      break;
    default:
      setTitle("New File");
      break;
  }
}

void NewFilesPage::setLabelText(QString text) {
  folderNameLabel->setText(text);
}

void NewFilesPage::setErrorText(QString text) {
  errorLabel->setText(text);
}

void NewFilesPage::setAllowDot(bool ok) {
  allowDot = ok;
}

/****************************
*****************************/
